package top.loveeveryone.configPage.document;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.util.Iterator;

/**
 * @author 29023
 */
@Component
public class Serverconfig {
    /**
     * 读取xml文件并返回内容 (查)
     * @param uri 文件路径
     * @return Document
     * @throws DocumentException 抛出错误，需要捕获
     */
    public Document parse(String uri) throws DocumentException{
        SAXReader reader = new SAXReader();
        return reader.read(uri);
    }

    /**
     *(增) 暂时不用
     * @return
     */
    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root");

        Element author1 = root.addElement("author")
                .addAttribute("name", "James")
                .addAttribute("location", "UK")
                .addText("James Strachan");

        Element author2 = root.addElement("author")
                .addAttribute("name", "Bob")
                .addAttribute("location", "US")
                .addText("Bob McWhirter");

        return document;
    }

    /**
     *
     * @param document
     * @throws DocumentException
     */
    public JSONArray bar(Document document) throws DocumentException {

        Element root = document.getRootElement();
        JSONObject jsonObject;
        JSONArray jsonArray = new JSONArray();
        for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
            jsonObject = new JSONObject();
            Element element = it.next();
            Attribute attribute = element.attribute(0);
            String value = attribute.getValue();
            Attribute attribute1 = element.attribute(1);
            String value1 = attribute1.getValue();
            jsonObject.put("name",value);
            jsonObject.put("value",value1);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    /**
     * 保存文件
     * @param document
     * @param uri
     * @throws Exception
     */
    public void outFile(Document document,String uri) throws Exception{
        FileWriter out = null;
        out = new FileWriter(uri);
        document.write(out);
        out.close();
    }

    /**
     * 搜索，利用xPath定位
     * @param document 想要返回document
     * @param xPath 例："/ServerSettings/property[@name='ServerName']" OR "//*[@name='ServerName']"
     */
    public String selectTranslate(Document document,String xPath){
        Node node = document.selectSingleNode(xPath);
        Element element = (Element)node;
        Attribute attribute = element.attribute(1);
        return attribute.getValue();
    }

    /**
     * 改，利用xPath定位
     * @param document document 想要返回document
     * @param xPath 例："/ServerSettings/property[@name='ServerName']" OR "//*[@name='ServerName']"
     * @param data 修改的内容
     * @return 返回整个xml
     */
    public Document upDataXml(Document document,String xPath,String data){
        Node node = document.selectSingleNode(xPath);
        Element element = (Element)node;
        Attribute attribute = element.attribute("value");
        attribute.setValue(data);
        return document;
    }
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        Serverconfig serverconfig = new Serverconfig();
        Document document = null;
        try {
            document = serverconfig.parse("..\\serverconfig.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
        }
//        Element rootElement = document.getRootElement();
//        Iterator iterator1 = rootElement.attributeIterator();
//        Iterator iterator = rootElement.elementIterator();
//        Object next = iterator.next();
//        Object next1 = iterator.next();
//
//        Class<? extends Iterator> aClass = iterator.getClass();
        Node node = document.selectSingleNode("//*[@name='ServerName']");
        Element element = (Element)node;
        Attribute attribute = element.attribute(0);
        attribute.setValue("换了一个内容");
        System.out.println("name");
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = serverconfig.bar(document);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

//        FileWriter out = null;
//        try {
//            out = new FileWriter("..\\foo.xml");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            document.write(out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            out.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        System.out.println("创建成功");
    }
}
